<template>
  <div class="box">
    <van-search v-model="value" clearable placeholder="请输入搜索关键词" @search="handleSearch" />

    <div class="checkSheep">
      <div class="left">
        <div class="navs" v-for="(item, index) in navs1" :key="item.id">
          <div class="nav_item" :class="{ on: currentNav1 === index }" @click="currentNav1 = index">
            <img src="@/assets/img/check.png" alt="" class="bian" :class="{ on: currentNav1 === index }" />
            <p>{{ item.name }}</p>
            <span>({{ item.count }})</span>
          </div>
          <div class="checkbox" :class="{ on: currentNav1 === index }">
            <van-checkbox
              @click="check1(index, item)"
              icon-size="18px"
              v-model="item.check"
              checked-color="#37B5B5"
            ></van-checkbox>
          </div>
        </div>
      </div>
      <div class="right">
        <div class="nos">
          <div
            class="no_item"
            @click="check2(index, item)"
            :class="{ on: item.check }"
            v-for="(item, index) in data2"
            :key="item.id"
          >
            <div class="n1">
              <div class="sex" :class="{ on: item.sex === '公' }">{{ item.sex }}</div>
              <p>{{ item.earLabelNo }}</p>
            </div>
            <div class="n2">
              <p>项圈号：{{ item.necklaceNo }}</p>
            </div>
            <div class="n2">
              <p>月龄：{{ item.monthNum }}</p>
            </div>
          </div>
        </div>
      </div>
      <div class="btn" @click="onSave">确定（{{ totalNumber.length }}）</div>
    </div>
  </div>
</template>

<script>
import { Checkbox, CheckboxGroup, Search, Toast } from 'vant'
import { query4Select, listPage } from '@/api/record'
import { setValue, getValue } from '@/utils/auth'

export default {
  components: {
    [Checkbox.name]: Checkbox,
    [CheckboxGroup.name]: CheckboxGroup,
    [Search.name]: Search,
    [Toast.name]: Toast
  },
  name: 'CheckSheep',
  data() {
    return {
      navs1: [], // 圈列表---原始数据
      navs2: [], // 羊列表---原始数据
      currentNav1: null, // 当前选中侧边栏
      value: '',
      data2: [], // 羊列表---展示数据
      sex: null, // 性别
      hourseId: null // 舍id
    }
  },
  created() {
    this.hourseId = getValue('fzmHourseId') || ''
    this.sex = getValue('fzmSex') || ''

    this.loadData()
  },
  computed: {
    totalNumber() {
      return this.navs2.filter(ele => {
        return ele.check
      })
    }
  },
  watch: {
    currentNav1() {
      const p = this.navs1[this.currentNav1].id
      this.data2 = this.navs2.filter(ele => {
        return ele.houseInfoId === p
      })
    }
  },
  methods: {
    async loadData() {
      const params = {
        houseInfoId: this.hourseId
      }
      await query4Select(params).then(res => {
        if (res.status === 0) {
          const datas1 = res.data.houseInfoList
          const datas2 = res.data.individualInfoList
          datas1.forEach(ele => {
            ele.check = false
          })
          datas2.forEach(ele => {
            ele.check = false
          })
          this.currentNav1 = 0
          this.navs1 = datas1
          this.navs2 = datas2
          if (getValue('earTag2')) {
            const narr = JSON.parse(getValue('earTag2'))
            const arr1 = [] // 选中舍数组
            this.navs2.forEach(ele1 => {
              narr.forEach(ele2 => {
                if (ele1.id === ele2) {
                  ele1.check = true
                  arr1.push(ele1.houseInfoId)
                }
              })
            })
            this.navs1.forEach(ele => {
              arr1.forEach(ele2 => {
                if (ele.id === ele2) {
                  ele.check = true
                }
              })
            })
          }
        }
      })
    },
    // 左侧选中-单选
    handleSingle(index, { check }) {
      this.currentNav1 = index
      this.navs1.forEach(ele => {
        ele.check = false
      })
      this.navs2.forEach(ele => {
        ele.check = false
      })
      this.navs1[index].check = check
      this.data2[0].check = check
    },
    // 左侧选中
    check1(index, { check, id }) {
      this.navs1[index].check = check
      this.navs2.forEach(ele => {
        if (ele.houseInfoId === id) {
          ele.check = check
        }
      })
    },
    // 右侧选中
    check2(index, { check, id }) {
      this.data2[index].check = !check
      this.navs2.forEach(ele => {
        if (ele.id === id) {
          ele.check = !check
        }
      })
      this.navs1[this.currentNav1].check = this.data2.some(ele => {
        return ele.check
      })
    },
    // 确定
    onSave() {
      const sheepList = this.totalNumber.map(ele => ele.id)
      console.log(sheepList)
      if (sheepList.length) {
        setValue('earTag2', JSON.stringify(sheepList))
        this.$bridge.call('base.closePage')
      } else {
        Toast('请选择')
      }
    },
    // 搜索框确定
    handleSearch() {
      Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      if (!this.value) {
        const p = this.navs1[this.currentNav1].id
        this.data2 = this.navs2.filter(ele => {
          return ele.houseInfoId === p
        })
        Toast.clear()
      } else {
        const data = {
          houseInfoId: this.hourseId, // 舍id
          noKeyWords: this.value
        }
        listPage(data).then(res => {
          if (res.status === 0) {
            res.data.rows.forEach(ele => {
              ele.check = false
            })

            this.navs2.forEach(ele1 => {
              res.data.rows.forEach(ele2 => {
                if (ele1.id === ele2.id) {
                  ele2.check = ele1.check
                }
              })
            })
            this.data2 = res.data.rows
          }
          Toast.clear()
        })
      }
    }
  }
}
</script>
<style lang='scss' scoped>
::v-deep .van-search {
  height: 50px;
  overflow: hidden;
}
::v-deep .van-search__content {
  background-color: #f7f7f7;
  border-radius: 20px;
}
.checkSheep {
  display: flex;
  height: calc(100vh - 110px);
  .btn {
    width: 100%;
    height: 50px;
    background-color: #fff;
    line-height: 50px;
    text-align: center;
    font-size: 16px;
    color: #37b5b5;
    position: fixed;
    left: 0;
    bottom: 0;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.5);
  }
  .left {
    width: 165px;
    height: 100%;
    overflow-y: auto;
    background-color: #f7f7f7;
    .navs {
      display: flex;
      min-height: 50px;
    }
    .checkbox {
      width: 20%;
      display: flex;
      // height: 100%;
      align-items: center;
    }
    .checkbox.on {
      background-color: #fff;
    }
    .nav_item {
      width: 80%;
      // height: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-right: 10px;
      box-sizing: border-box;
      color: #999999;
      p {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 14px;
        text-align: center;
      }
      .bian {
        opacity: 0;
      }
      .bian.on {
        opacity: 1;
      }
      div:last-child {
        // justify-content: space-between;
        justify-content: space-around;
      }
    }
    .nav_item.on {
      background-color: #fff;
      color: #333333;
    }
  }
  .right {
    width: calc(100vw - 145px);
    background-color: #fff;
    height: 100%;
    overflow-y: auto;
    .title {
      padding: 15px;
      box-sizing: border-box;
      display: flex;
      justify-content: flex-end;
      color: #333;
      font-size: 14px;
      span {
        margin-left: 15px;
      }
    }
    .nos {
      padding: 0 15px;
      padding-top: 10px;
      box-sizing: border-box;
      .no_item.on {
        background-color: #f5fbfb;
        border-color: #37b5b5;
      }
      .no_item {
        width: 100%;

        padding: 8px 10px 10px 10px;
        border: 1px solid #cccccc;
        margin-bottom: 10px;
        border-radius: 6px;
        position: relative;
        z-index: 1;
        .sex {
          width: 20px;
          height: 20px;
          margin-right: 6px;
          position: absolute;
          left: 0;
          top: 0;
          z-index: 2;
          background-color: rgba(242, 157, 157, 0.15);

          color: #f29d9d;
          text-align: center;
          line-height: 20px;
          border-radius: 6px 0px 10px 0px;
        }
        .sex.on {
          background-color: rgba(55, 181, 181, 0.15);
          color: #37b5b5 !important;
        }
        .n1 {
          display: flex;
          align-items: center;
          color: #333;
          font-size: 14px;
          margin-bottom: 6px;
          p {
            width: 100%;
            text-align: center;
          }
        }
        .n2 {
          width: 100%;
          height: 16px;
          display: flex;
          color: #999999;
          font-size: 12px;
          align-items: center;
          justify-content: space-between;
          div {
            width: 1px;
            background-color: #999999;
            height: 100%;
          }
        }
      }
    }
  }
}
</style>
